/**
 * Created by levy on 16/6/15.
 */

/**
 * @desc 下拉组件
 * @param conRender 渲染内容组件函数
 * @param endCallBack 下拉完成回调函数
 */
var PullRefresh = React.createClass({
    getInitialState: function () {
        return {};
    },
    getDefaultProps: function () {
        return {
            endCallBack: '',
            refreshCon: ''
        }
    },
    componentWillMount: function () {
        mui.plusReady(function () {
            plus.webview.currentWebview().css({
                scrollIndicator: 'none'
            });
        });
    },
    componentDidMount: function () {

        var me = this;
        window.liefeng.utils.pullRefresh({
            container: '#pullrefresh',
            //下拉顶部刷新，有默认函数
            callback: this.props.endCallBack || me.defaultPulldownFn,
            pullupFn: me.props.pullupFn,
        });

        //固定内容
        var fixedContent = this.props.fixed,
            $scroll = $('.mui-scroll');

        if(fixedContent){
            var $fixed = $scroll.find(fixedContent),
                getTranslateY = function (transform) {
                    let scrollTop = transform.split(',', 2)[1].split('px')[0];
                    return scrollTop > 0 ? 0 : parseInt(scrollTop) * -1;
                };

            $scroll.on('drag dragstart dragend', function () {
                //$fixed[0].style.WebkitTransform = 'translateY(' + getTranslateY($scroll.css('transform')) + 'px)';
                $fixed[0].style.cssText = '-webkit-transform: translate3d(0,' + getTranslateY($scroll.css('transform')) + 'px, 0) translateZ(0)';
            });

            $scroll.on('dragend', function () {
                //alert($fixed[0].style.cssText)
            })
        }
    },
    defaultPulldownFn: function () {
        setTimeout(function () {
            mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //refresh completed
            location.reload();
        }, 500);
    },
    render: function () {
        var update_con = '';
        if(this.props.conRender)
            update_con = this.props.conRender(this.props.refreshCon);
        //add by levy 可以直接传data,因为不想仅仅只是为了传个render而多写一个组件,
        else
            update_con = this.props.data;
        return (
            <div id="pullrefresh" className="mui-content mui-scroll-wrapper">
                <div className="mui-scroll">
                    {update_con}
                </div>
            </div>
        )
    }
});

export default PullRefresh;
